var mongoose = require('mongoose');
var assert = require('assert');

var Leadership = require('./models/leadership');

//Connection URL
var url = 'mongodb://localhost:27017/conFusion';
mongoose.connect(url);

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'console error: '))

db.once('open', function() {
    //we're connected!
    console.log('Connected correctly to server');

    Leadership.create({
        "name": "Peter Pan",
        "image": "images/alberto.png",
        "designation": "Chief Epicurious Officer",
        "abbr": "CEO",
        "description": "Our CEO, Peter, . . ."
    }, function(err, leadership) {
        if (err) throw err;

        console.log('leadership created!');
        console.log(leadership);

        var id = leadership._id;

        console.log('\n\n\n');
        //get all the leadership
        setTimeout(function() {
            Leadership.findByIdAndUpdate(id, {
                    $set: {
                        description: "Updated Our CEO,...",
                    }
                }, {
                    new: true
                })
                .exec(function(err, leadership) {
                    if (err) throw err;
                    console.log('Updated description!!!');
                    console.log(leadership);

                    console.log('\n\n\n');

                    db.collection('leadership').drop(function() {
                        db.close();
                    });
                });
        });
    });
})